package com.qaml.mes.device.repository.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qaml.mes.device.dao.EqpAttachmentDO;
import com.qaml.mes.device.dao.mapper.EqpAttachmentMapper;
import com.qaml.mes.device.repository.EqpAttachmentRepository;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * @ClassName EqpAttachmentRepositoryImpl
 * @Description TODO
 * @Author leikai
 * @Date 2024/2/27 0027 9:55
 * @Version 1.0
 */
@Repository
public class EqpAttachmentRepositoryImpl implements EqpAttachmentRepository {

    @Resource
    private EqpAttachmentMapper eqpAttachmentMapper;

    /**
     * 根据设备id删除设备附件
     *
     * @param id
     */
    @Override
    public void deleteByEqpId(String id) {
        LambdaQueryWrapper<EqpAttachmentDO> deleteQueryWrapper = new LambdaQueryWrapper<>();
        deleteQueryWrapper.eq(EqpAttachmentDO::getIsDeleted, 0);
        deleteQueryWrapper.eq(EqpAttachmentDO::getEqpId, id);
        eqpAttachmentMapper.delete(deleteQueryWrapper);
    }

    @Override
    public void saveBatch(List<EqpAttachmentDO> eqpAttachmentDOS) {
        eqpAttachmentMapper.saveBatch(eqpAttachmentDOS);
    }

    /**
     * 根据设备id查询设备附件
     *
     * @param id
     * @return
     */
    @Override
    public List<EqpAttachmentDO> getByEqpId(String id) {
        LambdaQueryWrapper<EqpAttachmentDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EqpAttachmentDO::getIsDeleted, 0);
        queryWrapper.eq(EqpAttachmentDO::getEqpId, id);
        List<EqpAttachmentDO> eqpAttachmentDOS = eqpAttachmentMapper.selectList(queryWrapper);

        if (eqpAttachmentDOS == null || eqpAttachmentDOS.isEmpty()) {
            return Collections.emptyList();
        }
        return eqpAttachmentDOS;
    }


    /**
     * 根据设备id和类型查询设备附件以及设备图片
     *
     * @param
     * @param type
     * @return
     */
    @Override
    public List<EqpAttachmentDO> getByEqpIdAndStatus(String id, String type) {
        LambdaQueryWrapper<EqpAttachmentDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EqpAttachmentDO::getIsDeleted, 0);
        queryWrapper.eq(EqpAttachmentDO::getEqpId, id);
        queryWrapper.eq(EqpAttachmentDO::getAttachmentType, type);
        return eqpAttachmentMapper.selectList(queryWrapper);
    }

    @Override
    public List<EqpAttachmentDO> getByEqpIdListAndType(List<String> eqpIds, String type) {
        LambdaQueryWrapper<EqpAttachmentDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EqpAttachmentDO::getIsDeleted, 0);
        queryWrapper.in(EqpAttachmentDO::getEqpId, eqpIds);
        queryWrapper.eq(EqpAttachmentDO::getAttachmentType, type);
        queryWrapper.eq(EqpAttachmentDO::getIsShow, 1);
        return eqpAttachmentMapper.selectList(queryWrapper);
    }

    /**
     * 根据设备附件Ids修改设备附件IsDeleted
     *
     * @param ids
     */
    @Override
    public void updateEqpAttachmentByIds(List<String> ids) {
        if (CollectionUtil.isEmpty(ids)) {
            return;
        }
        eqpAttachmentMapper.updateEqpAttachmentByIds(ids);
    }


}